﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarvellousWorks.PracticalPattern.Concept.Configurating
{
    public class NamedConfigurationElementCollectionBase<T> : ConfigurationElementCollection
        where T :NamedConfigurationElementBase,new()
    {
        // 外部通过 index 获取集合中特定的 configuration element
        public T this[int index]
        {
            get {  return (T)base.BaseGet(index); }
        }

        public new T this[string name]
        {
            get { return (T)(base.BaseGet(name)); }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new T();
        }

        /// <summary>
        /// 获取集合中某个特定 NamedConfigurationElementBase 的 key （Name 属性）
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as T).Name;
        }
    }
}
